package com.mitrais.training.yoga;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Yoga_K Description : Employee class contains an information about
 *         Name, Salary, Hire Date of Employee
 */
public class Employee implements Comparable<Employee> {
	private String name;
	private double salary;
	private Date hireDate;

	/**
	 * @param _name
	 *            the name to set
	 * @param _hireDate
	 *            the hire date to set
	 * @param _salary
	 *            the salary to set Description : method to raise salary of
	 *            Employee
	 */
	public Employee(String _name, Date _hireDate, Double _salary) {
		salary = (_salary == null) ? 0.0 : _salary;
		name = _name;
		hireDate = _hireDate;
	}

	public Employee() {
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the salary
	 */
	public double getSalary() {
		return salary;
	}

	/**
	 * @return the hireDate
	 */
	public Date getHireDate() {
		return hireDate;
	}

	/**
	 * @param percentage
	 *            the percentage to set Description : method to raise salary of
	 *            Employee
	 */
	public void raiseSalary(int percentage) {
		salary = salary + (salary * (percentage / 100));
		return;
	}

	@Override
	/*
	 * @param otherEmp to add other object of Employee Description :
	 */
	public int compareTo(Employee otherEmp) {
		// TODO Auto-generated method stub
		return compareAlphanumeric(this.name, otherEmp.name);
	}

	private int compareAlphanumeric(Object obj1, Object obj2) {

		String firstString = obj1.toString();
		String secondString = obj2.toString();

		if (secondString == null || firstString == null) {
			return 0;
		}

		int lengthFirstStr = firstString.length();
		int lengthSecondStr = secondString.length();

		int index1 = 0;
		int index2 = 0;

		while (index1 < lengthFirstStr && index2 < lengthSecondStr) {
			char ch1 = firstString.charAt(index1);
			char ch2 = secondString.charAt(index2);

			char[] space1 = new char[lengthFirstStr];
			char[] space2 = new char[lengthSecondStr];

			int loc1 = 0;
			int loc2 = 0;

			boolean isBreak = false;
			space1[loc1++] = ch1;
			index1++;

			if (index1 < lengthFirstStr) {
				ch1 = firstString.charAt(index1);
			} else {
				isBreak = true;
			}

			if (!isBreak) {
				while (Character.isDigit(ch1) == Character.isDigit(space1[0])) {
					space1[loc1++] = ch1;
					index1++;

					if (index1 < lengthFirstStr) {
						ch1 = firstString.charAt(index1);
					} else {
						break;
					}
				}
			}

			isBreak = false;
			space2[loc2++] = ch2;
			index2++;

			if (index2 < lengthSecondStr) {
				ch2 = secondString.charAt(index2);
			} else {
				isBreak = true;
			}

			if (!isBreak) {
				while (Character.isDigit(ch2) == Character.isDigit(space2[0])) {
					space2[loc2++] = ch2;
					index2++;

					if (index2 < lengthSecondStr) {
						ch2 = secondString.charAt(index2);
					} else {
						break;
					}
				}
			}

			String str1 = new String(space1);
			String str2 = new String(space2);

			int result;

			if (Character.isDigit(space1[0]) && Character.isDigit(space2[0])) {
				BigDecimal firstNumberToCompare = new BigDecimal(str1.trim());
				BigDecimal secondNumberToCompare = new BigDecimal(str2.trim());
				result = firstNumberToCompare.compareTo(secondNumberToCompare);
			} else {
				result = str1.compareTo(str2);
			}

			if (result != 0) {
				return result;
			}
		}
		return lengthFirstStr - lengthSecondStr;
	}
}
